package com.asset.dispoal.user.service.commom;

import com.asset.dispoal.user.domain.db.entity.BsLawyerDo;
import com.asset.dispoal.user.domain.db.mapper.SysUserMapper;
import com.asset.dispoal.user.dto.BsLawyerUpdateRequest;
import com.asset.dispoal.user.dto.UserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

/**
 * @author qr
 * @date 2022/5/12$
 */
@Component
public class UpdateLawyerCommand implements BaseCommand<BsLawyerUpdateRequest, Result> {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    UpdateUserCommand updateUserCommand;

    private UserRequest initUser(BsLawyerUpdateRequest request) {
        UserRequest userRequest = new UserRequest();
        userRequest.setId(request.getId());
        userRequest.setNickname(request.getNickname());
        userRequest.setAccount(request.getAccount());
        userRequest.setPassword(request.getPassword());
        userRequest.setMobile(request.getMobile());
        userRequest.setCompanyId(request.getCompanyId());
        userRequest.setDepIds(request.getDepIds());
        userRequest.setRoleIds(request.getRoleIds());
        userRequest.setState(request.getState());
        userRequest.setType("1");
        return userRequest;
    }

    @Override
    public Result execute(BsLawyerUpdateRequest bsLawyerRequest) {
        UserRequest userRequest = initUser(bsLawyerRequest);
        Result<String> result = updateUserCommand.execute(userRequest);
        if (result.isOk()) {
            BsLawyerDo bsLawyerDo = new BsLawyerDo();
            bsLawyerDo.setId(bsLawyerRequest.getId());
            bsLawyerDo.setAccountNo(bsLawyerRequest.getAccountNo());
            bsLawyerDo.setOpenBank(bsLawyerRequest.getOpenBank());
            bsLawyerDo.setLawFirmId(bsLawyerRequest.getLawFirmId());
            bsLawyerDo.setLevel(bsLawyerRequest.getLevel());
            bsLawyerDo.setNo(bsLawyerRequest.getNo());
            bsLawyerDo.setRemark(bsLawyerRequest.getRemark());
            sysUserMapper.updateLawyer(bsLawyerDo);
            sysUserMapper.deleteLawyerCourtByLawId(bsLawyerDo.getId());
            if (CollectionUtil.isNotEmpty(bsLawyerRequest.getCourtIdList())) {
                for (String courtId : bsLawyerRequest.getCourtIdList()) {
                    sysUserMapper.insertLawyerCourt(bsLawyerDo.getId(), courtId);
                }
            }
        }
        return Result.ok();
    }
}
